#### Appendix 7: Probing the Deliberation Mechanism ####

source("./code/loadPackages.R") # Install and load all necessary packages

# Load relevant data
de = fread("./data/deliberationData.csv", encoding="UTF-8")
de = de %>% dplyr::select(segID, everything())

nscd_all = fread("./data/meetingData.csv")
nscd_all$admin = factor(nscd_all$admin, levels=c("Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan"))
nscd_all$date = as.Date(nscd_all$date)

# Remove meetings that do not have the president in them
de = de |> filter(meetNum %in% unique(nscd_all$meetNum[which(nscd_all$presAttend==1)]))

# Unique Meetings
length(unique(de$meetNum)) # 258

# How many speech acts?
nrow(de) # 10,682 

## Split speech acts between the president and advisers
presSpeech = de %>% filter((admin=="Truman" & firstlast=="Harry Truman") | 
         (admin=="Eisenhower" & firstlast=="Dwight Eisenhower") |
         (admin=="Kennedy" & firstlast=="John Kennedy") |
         (admin=="Johnson" & firstlast=="Lyndon Johnson") |
         (admin=="Nixon" & firstlast=="Richard Nixon") |
         (admin=="Ford" & firstlast=="Gerald Ford") |
         (admin=="Carter" & firstlast=="Jimmy Carter") |
         (admin=="Reagan" & firstlast=="Ronald Reagan"))

advSpeech = de %>% filter(!(segID %in% presSpeech$segID))


#### 7.1: Seeking Counsel and Expressing Dissent ####

# Information search
sum(presSpeech$infosearch, na.rm=T)/nrow(presSpeech) # 0.309
sum(advSpeech$infosearch, na.rm=T)/nrow(advSpeech)   # 0.111

# Dissent
sum(presSpeech$dissent, na.rm=T)/nrow(presSpeech) # 0.105
sum(advSpeech$dissent, na.rm=T)/nrow(advSpeech)   # 0.151

# What percentage of meetings have dissent?
meetDiss = de %>% group_by(meetNum) %>% summarize(diss=sum(dissent, na.rm=T))
sum(meetDiss$diss>0)/length(unique(de$meetNum))   # 0.632


#### Figure A16: Deliberation Patterns across Administrations ####

delib_admin = count(de, vars = admin)
dissent = aggregate(dissent ~ admin, data = de, sum)
info = aggregate(infosearch ~ admin, data = de, sum)
delib_admin = merge(delib_admin, dissent, by.x = "vars", by.y = "admin")
delib_admin = merge(delib_admin, info, by.x = "vars", by.y = "admin")
delib_admin$dissent.per = delib_admin$dissent / delib_admin$n
delib_admin$infosearch.per = delib_admin$infosearch / delib_admin$n
delib_admin = delib_admin %>% dplyr::select(admin=vars, dissent.per, infosearch.per)
delib_admin = pivot_longer(delib_admin, cols=c("dissent.per", "infosearch.per"))
delib_admin$admin = as.factor(delib_admin$admin)

delib_admin$admin = factor(delib_admin$admin, levels=c("Truman", "Eisenhower", "Kennedy", "Johnson", 
                                                     "Nixon", "Ford", "Carter", "Reagan"))

# Make plot
delibPlot <- ggplot(delib_admin, aes(x=admin, y=value, fill=name)) +
  geom_bar(stat="identity", width=0.75, position=position_dodge()) + theme_minimal() + xlab("") + 
  ylab("Percent of Speech Acts") + scale_fill_manual(values=c("gray30", "gray60"), labels = c("Dissent", "Information Search")) + 
  theme(legend.position="bottom",
        legend.title=element_blank(), legend.margin=margin(-12.5,0,0,0),
        plot.title = element_text(hjust = 0.5))
print(delibPlot)
ggsave("./figures/deliberation.pdf", plot = delibPlot, width = 5.5, height = 3)



#### Figure 5: Leader Search for Counsel during Meetings ####

# Renumber speech acts
delib = de %>% group_by(meetNum) %>% dplyr::mutate(segNumber = row_number())
delib$segNumberNext = delib$segNumber + 1

# How many times does the president speak?
presnames = c("Harry Truman", "Dwight Eisenhower", "John Kennedy", "Lyndon Johnson", 
              "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan")
preslasts = c("Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan")

presSpeak = NA
for (i in 1:nrow(delib)) {
  onerow = delib[i,]
  onepres = presnames[grep(onerow$admin, preslasts)]
  onespeaker = onerow$firstlast
  
  if (onespeaker==onepres) {
    presSpeak[i] = 1
  } else {
    presSpeak[i] = 0
  }
}
delib$presSpeak = presSpeak
delib$presSearch = ifelse(presSpeak==1 & delib$infosearch==1, 1, 0) # Whether the president searched for info

# Now, need to create data for survival plots
# When did the president seek information?
dmn = unique(delib$meetNum)
qList = list()
for (i in 1:length(dmn)) {
  onemeet = delib[which(delib$meetNum==dmn[i]),]  
  
  # Was president there/Did he speak at all?
  presThere = ifelse(sum(onemeet$presSpeak)>0, 1, 0)
  
  # Did president ask a question?
  prq = which(onemeet$presSearch==1)
  
  # If not, note this
  if (length(prq)==0) {
    pq = 0
    time = sum(onemeet$presSpeak)
  } else {
    pq = 1
    time = sum(onemeet$presSpeak[1:min(prq)])
  }
  
  # Create necessary data
  qList[[i]] = data.frame(pq, time, admin=onemeet$admin[1], meetNum=onemeet$meetNum[1], presThere)
  
}
qdata = do.call("rbind", qList)
qdata$admin = factor(qdata$admin, levels=c("Truman", "Eisenhower", "Kennedy", "Johnson", 
                                           "Nixon", "Ford", "Carter", "Reagan"))
qdata = qdata[which(qdata$presThere==1),]

# Make plot
qfit = survfit(Surv(time, pq) ~ admin, data=qdata)
counselPlot = ggplot2::autoplot(qfit, conf.int=F, surv.size=1, censor.shape="") + theme_bw() + 
  xlab("Number of speech acts by president per meeting") + ylab("Probability of president not seeking counsel") +
  scale_color_discrete("President") + facet_wrap(~strata, nrow=2) + theme(legend.position = "none")

print(counselPlot)
ggsave("./figures/presCounsel.pdf", plot=counselPlot, width = 7.5, height = 3.75)

# Clear environment
rm(list = ls())
